﻿using Database.Context;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;


namespace QuanLyThongTinCoSo.Forms.PhieuDieuTraCoSo
{
    public partial class frmHangMuc : frmDetail
    {
        public frmHangMuc()
        {
            InitializeComponent();
            LoadBacChiuLua();
            LoadHangSanXuat();
        }

        public frmHangMuc(int id)
        {
            InitializeComponent();
            Text = @"Sửa Hạng mục";
            _fromtype = Constant.TypeForm.Edit;
            _id = id;

            LoadBacChiuLua();
            LoadHangSanXuat();

            var biz = new HangMucBiz();
            var model = biz.GetItemById(id) as Model.HangMuc;
            if (model == null) return;
            
            txtTenCongTrinh.Text = model.TenCongTrinh;

            drlHangSanXuat.SelectedValue = model.HangSanXuat.Id;
            drlBacChiuLua.SelectedValue = model.BacChiuLua.Id;
            if (model.DienTichM2 != null) txtDienTichM2.Value = (decimal) model.DienTichM2;
            if (model.SoTang != null) txtSoTang.Value = (decimal) model.SoTang;
            if (model.SoPhong != null) txtSoPhong.Value = (decimal) model.SoPhong;
            txtTenChatChay.Text = model.TenChatChay;
            if (model.DienTichChatChayM2 != null) txtDienTichChatChatM2.Value = (decimal) model.DienTichChatChayM2;
            if (model.DienTichM3 != null) txtDienTichM3.Value = (decimal) model.DienTichM3;
        }

        private void LoadBacChiuLua()
        {
            var biz = new BacChiuLuaBiz();
            drlBacChiuLua.DataSource = biz.GetAll();
            drlBacChiuLua.DisplayMember = "Ten";
            drlBacChiuLua.ValueMember = "ID";
        }

        private void LoadHangSanXuat()
        {
            var biz = new HangSanXuatBiz();
            drlHangSanXuat.DataSource = biz.GetAll();
            drlHangSanXuat.DisplayMember = "Ten";
            drlHangSanXuat.ValueMember = "ID";
        }

        #region Validation

        protected override bool Validation()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtTenCongTrinh.Text))
            {
                errorProvider.SetError(txtTenCongTrinh, "Bạn chưa nhập Tên công trình. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert

        protected override void Insert()
        {
            var context = new Context();
            var hangSanXuatBiz = new HangSanXuatBiz(context);
            var bacChiuLuaBiz = new BacChiuLuaBiz(context);
            var model = new Model.HangMuc();
            
            var hangSanXuatId = (int)drlHangSanXuat.SelectedValue;
            model.HangSanXuat = hangSanXuatBiz.GetItemById(hangSanXuatId) as Model.HangSanXuat;

            model.TenCongTrinh = txtTenCongTrinh.Text.Trim();
            model.DienTichM2 = (int?) txtDienTichM2.Value;
            model.DienTichM3 = (int?) txtDienTichM3.Value;
            model.SoTang = (int?) txtSoTang.Value;
            model.SoPhong = (int?) txtSoPhong.Value;
            model.TenChatChay = txtTenChatChay.Text.Trim();
            model.DienTichChatChayM2 = (int?) txtDienTichChatChatM2.Value;
            
            var bacChiuLuaId = (int)drlBacChiuLua.SelectedValue;
            model.BacChiuLua = bacChiuLuaBiz.GetItemById(bacChiuLuaId) as Model.BacChiuLua;

            var biz = new HangMucBiz(context);
            biz.Insert(model);
        }

        #endregion

        #region Edit

        protected override void Edit()
        {
            var context = new Context();
            var hangSanXuatBiz = new HangSanXuatBiz(context);
            var bacChiuLuaBiz = new BacChiuLuaBiz(context);
            var biz = new HangMucBiz(context);

            var item = biz.GetItemById(_id) as Model.HangMuc;
            if (item != null)
            {
                item.TenCongTrinh = txtTenCongTrinh.Text.Trim();
                item.DienTichM2 = (int?)txtDienTichM2.Value;
                item.DienTichM3 = (int?)txtDienTichM3.Value;
                item.SoTang = (int?)txtSoTang.Value;
                item.SoPhong = (int?)txtSoPhong.Value;
                item.TenChatChay = txtTenChatChay.Text.Trim();
                item.DienTichChatChayM2 = (int?)txtDienTichChatChatM2.Value;

                var hangSanXuatId = (int)drlHangSanXuat.SelectedValue;
                item.HangSanXuat = hangSanXuatBiz.GetItemById(hangSanXuatId) as Model.HangSanXuat;

                var bacChiuLuaId = (int)drlBacChiuLua.SelectedValue;
                item.BacChiuLua = bacChiuLuaBiz.GetItemById(bacChiuLuaId) as Model.BacChiuLua;

                biz.Update(item);
            }
        }

        #endregion
    }
}